home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Celestin Apprentice 5
/
Apprentice-Release5.iso
/
Source Code
/
Libraries
/
SAT 2.3.8
/
Demos
/
Bricks demo ƒ
/
Bricks.c
< prev
next >
Wrap
C/C++ Source or Header
|
1995-11-10
|
8KB
|
289 lines
//**************************************
//* C translation by Richard Bannister *
//* *
//* Titan Software *
//* 76 Stillorgan Wood *
//* Stillorgan, Co. Dublin *
//* Republic of Ireland *
//* *
//* Author of Zap'T'Balls *
//**************************************
//...with some modifications by Ingemar
// Bricks
//
// Demo program for SAT 2.3, by Ingemar Ragnemalm november 1994
//
// The purpose of this demo is to test and demonstrate the support for what I call "resting" sprites.
// If a sprite neither moves, changes face, overlaps a changing sprite nor changes its place in the
// sprite list, then SAT can avoid drawing it.
//
// For programs where all sprites change in every frame (which is the case in many arcade games)
// then this is not of any interest at all, and will just slow things down. For other programs, for
// example board games, Tetris-style games etc, this technique is very useful.
//
// For the moment (SAT 2.3d3), you get the new system by calling SATRun2 instead of SATRun. The name
// of SATRun2 is likely to change in the future.
//
// When you start Bricks, the sprites will not be initialied in order, so since VPositionSort
// is active, they will sort during the first frames. This slows the program down for the first
// few seconds. If this is a problem, it can be avoided by either turning off sorting or
// creating the sprite in the proper places from the start.
//
// Bug note: When using "fast graphics", the cursor will cause some "mouse droppings". This is best avoided
// by hiding the cursor and repllacing it with a SAT sprite. The "proper" way to avoid the problem is to
// call ShieldCursor, but that will make the cursor flicker a lot.
#include "SAT.h"
enum{
kNumBricks = 30
};
/* prototypes - really not necessary since all routines are in order */
void InitBricks();
pascal void HandleRestingBrick (SpritePtr me);
pascal void HandleFollowMouse (SpritePtr me);
pascal void SetupBrick (SpritePtr me);
void SynchMenus();
void MenuSelection (long theSelection);
static FacePtr brickFace[kNumBricks];
EventRecord myEvent;
WindowPtr whichWindow;
MenuHandle appleMenu, fileMenu;
Boolean gUseStagger, gDone, gUseFast, gAllowBackground;
short i;
SpritePtr sp, found;
long theSelection;
Point where;
short theKey;
short whichPart;
Boolean hasEvent;
void InitBricks()
{
for (i = 0; i < (kNumBricks); i++)
brickFace[i] = SATGetFace(i + 128);
}
pascal void HandleRestingBrick (SpritePtr me)
{
}
pascal void HandleFollowMouse (SpritePtr me)
{
Point mousePos;
if (Button())
{
GetMouse(&mousePos);
me->position.h = me->position.h + mousePos.h - me->speed.h;
me->position.v = me->position.v + mousePos.v - me->speed.v;
me->speed = mousePos;
}
else
{
me->task = &HandleRestingBrick;
}
}
pascal void SetupBrick (SpritePtr me)
{
me->task = &HandleRestingBrick;
me->face = brickFace[me->kind];
}
void SynchMenus()
{
CheckItem(fileMenu, 1, (!gUseStagger));
CheckItem(fileMenu, 2, gUseStagger);
CheckItem(fileMenu, 3, gUseFast);
CheckItem(fileMenu, 4, gAllowBackground);
}
void MenuSelection (long theSelection)
{
Str255 name;
GrafPtr saveport;
short theMenu, theItem;
theMenu=HiWord(theSelection);
theItem=LoWord(theSelection);
switch (theMenu)
{
case 128:
if (theItem == 1)
{
SATReportStr("\pExperimental SAT demo program. Resting sprites allow good speed with many sprites!");
}
else
{
GetPort(&saveport);
GetMenuItemText(appleMenu, 1, name); // Get Name
OpenDeskAcc(name); // Run the Desk Accessory
SetPort(saveport);
}
break;
case 129:
switch (theItem)
{
case 1:
gUseStagger = false;
break;
case 2:
gUseStagger = true;
break;
case 3:
gUseFast = !gUseFast;
break;
case 4:
gAllowBackground =!gAllowBackground;
break;
case 6:
gDone = true;
break;
}
break;
}
SynchMenus();
HiliteMenu(0);
}
main()
{
SATInitToolbox();
InitBricks();
SATInit(128, 129, 512, 342);
SATSetPortScreen();
gUseStagger = true;
gAllowBackground = true;
gUseFast = false;
for (i = 0; i < (kNumBricks); i++)
sp = SATNewSprite(i, SATRand(gSAT.offSizeH - 48),
SATRand(gSAT.offSizeV - 64), &SetupBrick);
for (i = 0; i < (kNumBricks); i++)
sp = SATNewSprite(i, SATRand(gSAT.offSizeH - 48),
SATRand(gSAT.offSizeV - 64), &SetupBrick);
appleMenu = NewMenu(128, "\p\024");
AppendMenu(appleMenu, "\pAbout Bricks demo…;(-;");
AppendResMenu(appleMenu, 'DRVR');
InsertMenu(appleMenu, 0); // put apple menu at end of menu bar
fileMenu = NewMenu(129, "\pFile");
AppendMenu(fileMenu, "\pRunSAT/1;RunSAT2/2;Fast graphics/F;Allow background tasks;(-;Quit/Q");
InsertMenu(fileMenu, 0); // put file menu at end of menu bar
DrawMenuBar();
SynchMenus();
InitCursor();
do
{
// if gUseFast then
// ShieldCursor(gSAT.bounds, Point(0));
if (gUseStagger)
SATRun2(gUseFast);
else
SATRun(gUseFast);
// if gUseFast then
// ShowCursor;
if (gAllowBackground)
{
SystemTask();
hasEvent = GetNextEvent(everyEvent, &myEvent);
}
else
{
hasEvent = GetOSEvent(everyEvent, &myEvent);
// or perhaps mDownMask + updateMask + keyDownMask
}
if (hasEvent)
{
switch (myEvent.what)
{
case updateEvt:
if ((WindowPtr)myEvent.message == gSAT.wind.port)
{
BeginUpdate(gSAT.wind.port);
SATRedraw();
EndUpdate(gSAT.wind.port);
}
break;
case keyDown:
{
theKey = myEvent.message &charCodeMask;
if ((myEvent.modifiers &cmdKey) != 0)
{
MenuSelection(MenuKey(theKey));
}
// else
// DoKey(theKey, theEvent.modifiers)
}
break;
case mouseDown:
{
whichPart = FindWindow(myEvent.where, &whichWindow);
switch (whichPart)
{
case inMenuBar:
{
theSelection = MenuSelect(myEvent.where);
MenuSelection(theSelection);
}
break;
case inSysWindow:
SystemClick(&myEvent, whichWindow);
break;
case inContent:
{
found = nil;
sp = gSAT.sRoot;
GetMouse(&where);
do
{
if (PtInRect(where, &sp->r)) found = sp;
sp = sp->next;
} while (sp != nil);
if (found != nil)
{
if (!(myEvent.modifiers == 0))
{
found->task = nil;
}
else
{
found->task = &HandleFollowMouse;
found->speed = where; // not speed, but storage for old mouse position
}
}
}
break;
}
}
}
}
} while (!(gDone));
SATSoundShutup();
}